package com.zretc.task;

import com.zretc.dao.OrdersMapper;
import com.zretc.dao.SalesMapper;
import com.zretc.entity.Orders;
import com.zretc.entity.Sales;
import com.zretc.entity.Screening;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 周文涛
 * 场次开始时结算订单，生成销售记录（30分钟结算一次）
 */
@Component
public class ScreeningStartTask {

    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private SalesMapper salesMapper;
    private static final Logger LOGGER = LoggerFactory.getLogger(ScreeningStartTask.class);

    @Scheduled(cron = "0 0/30 * * * ?")
    @Transactional
    public void screeningStart(){
        LOGGER.debug("=============正在定时结算订单信息，生成销售记录============");
        // 查询所有到点场次的订单（已支付的 状态为1的）
        List<String> ordersIdList = ordersMapper.getScreeningStart();

        for (String ordersId : ordersIdList) {
            // 修改状态
            ordersMapper.updateStatus(ordersId, 2);// 2为已完成
            // 生成销售记录
            Orders orders = ordersMapper.getOneOrder(ordersId);
            // 场次
            Screening screening = orders.getScreening();
            // 价格
            Float salesPrice = orders.getPayPrice();
            // 时间(场次开始时间)
            String salesTime = orders.getScreening().getScreeningStartTime();
            // 数量
            Integer salesCount = orders.getOrdersDetails().size();
            Sales sales = new Sales();
            sales.setScreening(screening);
            sales.setSalesCount(salesCount);
            sales.setSalesTime(salesTime);
            sales.setSalesPrice(salesPrice);
            salesMapper.insert(sales);
        }
        LOGGER.debug("=============定时结算订单信息完成============");

    }
}
